﻿using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using WebManager;
using System.Data.SqlClient;
namespace BaoJian.WebManager.Function.ClassifyProducts
{
    public partial class Add : System.Web.UI.Page
    {
        private string ColumnID = null;
        protected void Page_Load(object sender, EventArgs e)
        {
            ColumnID = HttpContext.Current.Request.QueryString["ColumnID"];
            if (!Page.IsPostBack)
            {
                BindData();
            }
        }

        private void BindData()
        {
            BindProduct();
            BindMainCatalog();
        }
        private void BindProduct()
        {
            LYQ.BLL.Product bll = new LYQ.BLL.Product();
            ProductList.DataSource = bll.GetAllList().Tables[0];
            ProductList.DataBind();
            ListItem item = new ListItem("请选择", "");
            ProductList.Items.Insert(0, item);
        }
        
        private void BindMainCatalog()
        {
           string sql="SELECT DISTINCT [MainCatalogName],[MainCatalogID] FROM [Class] ";
           MainCatalog.DataSource = Maticsoft.DBUtility.DbHelperSQL.Query(sql).Tables[0];
           MainCatalog.DataBind();
           ListItem item = new ListItem("请选择", "");
           MainCatalog.Items.Insert(0,item);
        }
        private void BindGridView()
        {
            LYQ.BLL.ClassifyProducts bll = new LYQ.BLL.ClassifyProducts();
            string where = string.Format("ProductID='{0}'",ProductList.SelectedValue);
            GridView1.DataSource=bll.GetList(where);
            GridView1.DataBind();
        }

        protected void ProductList_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (ProductList.SelectedValue == "")
                return;
            BindGridView();
            MainCatalog.SelectedIndex = 0;
            SubCatalog.Items.Clear();
            ListItem item = new ListItem("请选择", "");
            SubCatalog.Items.Insert(0, item);
        }

        protected void MainCatalog_SelectedIndexChanged(object sender, EventArgs e)
        {
            SubCatalog.Items.Clear();
            string sql = string.Format("SELECT [SubCatalogID],[SubCatalogName] FROM [Class] WHERE  [MainCatalogID]  ='{0}'", MainCatalog.SelectedValue);
            SubCatalog.DataSource = Maticsoft.DBUtility.DbHelperSQL.Query(sql);
            SubCatalog.DataBind();
            ListItem item = new ListItem("请选择", "");
            SubCatalog.Items.Insert(0, item);

        }

        protected void Save_Click(object sender, EventArgs e)
        {
            Label label = Page.Master.FindControl("lblTitle") as Label;
            if (string.IsNullOrEmpty(ProductList.SelectedValue)
                || string.IsNullOrEmpty(MainCatalog.SelectedValue)
                || string.IsNullOrEmpty(SubCatalog.SelectedValue))
            {
                label.Text = "请选择相应的选项";
                return;
            }

            string sql = string.Format("SELECT COUNT(*) AS Num FROM [ClassifyProducts] WHERE [MainCatalogID]='{0}' AND [SubCatalogID]='{1}'",
                MainCatalog.SelectedValue, SubCatalog.SelectedValue);

            bool exist = false;
            SqlDataReader reader = Maticsoft.DBUtility.DbHelperSQL.ExecuteReader(sql);
            if (reader.Read())
            {
                if (Convert.ToInt32(reader["Num"]) > 0)
                {
                    exist = true;
                }
            }
            reader.Close();

            if (exist)
            {
                label.Text = "该子分类已经存在";
                return;
            }

            LYQ.BLL.ClassifyProducts bll = new LYQ.BLL.ClassifyProducts();
            LYQ.Model.ClassifyProducts model = new LYQ.Model.ClassifyProducts();
            model.MainCatalogID = MainCatalog.SelectedValue;
            model.MainCatalogName = MainCatalog.SelectedItem.Text;
            model.SubCatalogID = SubCatalog.SelectedValue;
            model.SubCatalogName = SubCatalog.SelectedItem.Text;
            model.ProductID = ProductList.SelectedValue;
            model.ProductName = ProductList.SelectedItem.Text;
            bll.Add(model);
            string url = URLUtility.UrlBase + "/WebManager/Function/ClassifyProducts/List.aspx?oid=" + ColumnID;
            Page.Response.Redirect(url);
        }

        protected void Cancel_Click(object sender, EventArgs e)
        {
            string url = URLUtility.UrlBase + "/WebManager/Function/ClassifyProducts/List.aspx?oid=" + ColumnID;
            Page.Response.Redirect(url);
        }
          
    }
}
